import scrapy
from Hue.basepro import ZhengFuBaseSpider


class JianSpider(ZhengFuBaseSpider):
    name = 'Jian-pro'
    allowed_domains = ['jian.gov.cn']
    start_urls = ['http://http://www.jian.gov.cn//']
    api = 'http://www.jian.gov.cn/fullsearchs/keyword-{keyword}-size-20-siteid-21-inputorder-1-time-all-field-title-page-{page}.html'
    keywords = ['疫情防控']
    method = 'GET'

    def edit_page(self, response):
        all_result = response.css('div.result-num > span:first-child::text').get()
        all_page = int(all_result) // 20 + 1
        return int(all_page)

    def edit_items_box(self, response):
        items_box = response.xpath('//ul[@class="search-list"]')
        return items_box

    def edit_item(self, item):
        meta_info = {
            "title": item.css('li.search-title > a::attr(href)').get(),
            "url": item.css('li.search-title > a::attr(href)').get(),
            "pre_content": "".join(item.css('li.search-column *::text').getall()).strip(),
            "date": item.css('li.search-url > span::text, li.search-column > span::text').get().replace("发布日期：", "")
        }
        return meta_info
